﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Write.Api.Application.Command.RoleCommand;
using MediatR;
using SqlSugar;

namespace B.S.BaseData.Write.Api.Application.CommandHander.RoleHander
{
    public class DelRoleCommandHander : IRequestHandler<DelRoleCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public DelRoleCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 逻辑删除角色处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var list = db.Queryable<Role>().Where(x => x.RoleId == request.RoleId).First();

            if (list != null)
            {
                if (list.IsDelete == false)
                {
                    list.IsDelete = true;
                    db.Updateable<Role>(list).ExecuteCommandAsync();
                }
                else
                {
                    list.IsDelete = false;
                    db.Updateable<Role>(list).ExecuteCommandAsync();
                }
            }

            result.Code = ApiEnum.修改成功;
            result.Msg = ApiEnum.修改成功.ToString();
            return Task.FromResult(result);
        }
    }
}
